// 主要实现好友管理子服务的服务器的搭建
#include "friend_server.hpp"

// 定义命令行参数
DEFINE_bool(run_mode, false, "程序的运行模式，false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(registry_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(instance_name, "/friend_service/instance", "当前实例名称");
DEFINE_string(access_host, "127.0.0.1:10006", "当前实例的外部访问地址");

DEFINE_int32(listen_port, 10006, "Rpc服务器监听端口");
DEFINE_int32(rpc_timeout, -1, "Rpc调用超时时间");
DEFINE_int32(rpc_threads, 1, "Rpc的IO线程数量");

DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(user_service, "/service/user_service", "用户管理子服务名称");
DEFINE_string(message_service, "/service/message_service", "消息存储子服务名称");

DEFINE_string(es_host, "http://127.0.0.1:9200/", "ES搜索引擎服务器URL");

DEFINE_string(mysql_host, "127.0.0.1", "Mysql服务器访问地址");
DEFINE_string(mysql_user, "root", "Mysql服务器访问用户名");
DEFINE_string(mysql_pswd, "123456", "Mysql服务器访问密码");
DEFINE_string(mysql_db, "mag", "Mysql默认库名称");
DEFINE_string(mysql_cset, "utf8", "Mysql客户端字符集");
DEFINE_int32(mysql_port, 0, "Mysql服务器访问端口");
DEFINE_int32(mysql_pool_count, 4, "Mysql连接池最大连接数量");

int main(int argc, char *argv[])
{
    // 解析命令行参数
    google::ParseCommandLineFlags(&argc, &argv, true);

    // 初始化日志系统
    init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);
    
    // 确保日志系统初始化成功
    if (spdlog::default_logger() == nullptr) {
        std::cerr << "日志系统初始化失败！" << std::endl;
        return -1;
    }
    LOG_INFO("日志系统初始化成功！");

    // 创建 FriendServerBuilder 实例
    mag::FriendServerBuilder fsb;

    // 配置 ES 连接
    LOG_INFO("配置ES连接: {}", FLAGS_es_host);
    fsb.make_es_object({FLAGS_es_host});

    // 配置 MySQL 连接
    LOG_INFO("配置MySQL连接: user = {}, host = {}, db = {}, port = {}, pool_count = {}",
             FLAGS_mysql_user, FLAGS_mysql_host, FLAGS_mysql_db, FLAGS_mysql_port, FLAGS_mysql_pool_count);
    fsb.make_mysql_object(FLAGS_mysql_user, FLAGS_mysql_pswd, FLAGS_mysql_host, 
        FLAGS_mysql_db, FLAGS_mysql_cset, FLAGS_mysql_port, FLAGS_mysql_pool_count);

    // 配置服务发现对象
    LOG_INFO("配置服务发现对象: registry_host = {}, base_service = {}, user_service = {}, message_service = {}",
             FLAGS_registry_host, FLAGS_base_service, FLAGS_user_service, FLAGS_message_service);
    fsb.make_discovery_object(FLAGS_registry_host, FLAGS_base_service, FLAGS_user_service, FLAGS_message_service);

    // 配置 RPC 服务器
    LOG_INFO("配置RPC服务器: listen_port = {}, rpc_timeout = {}, rpc_threads = {}",
             FLAGS_listen_port, FLAGS_rpc_timeout, FLAGS_rpc_threads);
    fsb.make_rpc_server(FLAGS_listen_port, FLAGS_rpc_timeout, FLAGS_rpc_threads);

    // 配置服务注册对象
    LOG_INFO("配置注册对象: registry_host = {}, service_name = {}, access_host = {}",
             FLAGS_registry_host, FLAGS_base_service + FLAGS_instance_name, FLAGS_access_host);
    fsb.make_registry_object(FLAGS_registry_host, FLAGS_base_service + FLAGS_instance_name, FLAGS_access_host);

    // 构建并启动服务器
    LOG_INFO("构建并启动服务器...");
    auto server = fsb.build();
    if (server == nullptr) {
        LOG_ERROR("构建服务器失败！");
        return -1;
    }

    LOG_INFO("服务器启动成功，开始监听...");
    server->start();

    return 0;
}






// #include "friend_server.hpp"

// DEFINE_bool(run_mode, false, "程序的运行模式，false-调试； true-发布；");
// DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
// DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

// DEFINE_string(registry_host, "http://127.0.0.1:2379", "服务注册中心地址");
// DEFINE_string(instance_name, "/friend_service/instance", "当前实例名称");
// DEFINE_string(access_host, "127.0.0.1:10006", "当前实例的外部访问地址");

// DEFINE_int32(listen_port, 10006, "Rpc服务器监听端口");
// DEFINE_int32(rpc_timeout, -1, "Rpc调用超时时间");
// DEFINE_int32(rpc_threads, 1, "Rpc的IO线程数量");


// DEFINE_string(base_service, "/service", "服务监控根目录");
// DEFINE_string(user_service, "/service/user_service", "用户管理子服务名称");
// DEFINE_string(message_service, "/service/message_service", "消息存储子服务名称");

// DEFINE_string(es_host, "http://127.0.0.1:9200/", "ES搜索引擎服务器URL");

// DEFINE_string(mysql_host, "127.0.0.1", "Mysql服务器访问地址");
// DEFINE_string(mysql_user, "root", "Mysql服务器访问用户名");
// DEFINE_string(mysql_pswd, "123456", "Mysql服务器访问密码");
// DEFINE_string(mysql_db, "mag", "Mysql默认库名称");
// DEFINE_string(mysql_cset, "utf8", "Mysql客户端字符集");
// DEFINE_int32(mysql_port, 0, "Mysql服务器访问端口");
// DEFINE_int32(mysql_pool_count, 4, "Mysql连接池最大连接数量");

// int main(int argc, char *argv[])
// {
//     // 记录命令行参数解析开始
//     LOG_INFO("开始解析命令行参数...");
//     google::ParseCommandLineFlags(&argc, &argv, true);

//     // 初始化日志
//     LOG_INFO("初始化日志: run_mode = {}, log_file = {}, log_level = {}",
//              FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);
//     init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

//     mag::FriendServerBuilder fsb;
    
//     // 配置ES对象
//     LOG_INFO("配置ES连接: {}", FLAGS_es_host);
//     fsb.make_es_object({FLAGS_es_host});

//     // 配置MySQL对象
//     LOG_INFO("配置MySQL连接: user = {}, host = {}, db = {}, port = {}, pool_count = {}",
//              FLAGS_mysql_user, FLAGS_mysql_host, FLAGS_mysql_db, FLAGS_mysql_port, FLAGS_mysql_pool_count);
//     fsb.make_mysql_object(FLAGS_mysql_user, FLAGS_mysql_pswd, FLAGS_mysql_host, 
//         FLAGS_mysql_db, FLAGS_mysql_cset, FLAGS_mysql_port, FLAGS_mysql_pool_count);

//     // 配置服务发现对象
//     LOG_INFO("配置服务发现对象: registry_host = {}, base_service = {}, user_service = {}, message_service = {}",
//              FLAGS_registry_host, FLAGS_base_service, FLAGS_user_service, FLAGS_message_service);
//     fsb.make_discovery_object(FLAGS_registry_host, FLAGS_base_service, FLAGS_user_service, FLAGS_message_service);

//     // 配置RPC服务器
//     LOG_INFO("配置RPC服务器: listen_port = {}, rpc_timeout = {}, rpc_threads = {}",
//              FLAGS_listen_port, FLAGS_rpc_timeout, FLAGS_rpc_threads);
//     fsb.make_rpc_server(FLAGS_listen_port, FLAGS_rpc_timeout, FLAGS_rpc_threads);

//     // 配置注册对象
//     LOG_INFO("配置注册对象: registry_host = {}, service_name = {}, access_host = {}",
//              FLAGS_registry_host, FLAGS_base_service + FLAGS_instance_name, FLAGS_access_host);
//     fsb.make_registry_object(FLAGS_registry_host, FLAGS_base_service + FLAGS_instance_name, FLAGS_access_host);

//     // 构建并启动服务器
//     LOG_INFO("构建并启动服务器...");
//     auto server = fsb.build();
//     if (server == nullptr) {
//         LOG_ERROR("构建服务器失败！");
//         return -1;
//     }

//     LOG_INFO("服务器启动成功，开始监听...");
//     server->start();

//     return 0;
// }
